package pms;

import java.awt.Component;
import java.io.IOException;
import java.sql.Date;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import javax.swing.JFileChooser;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class PayrollAdmin extends Supervisor {

	public PayrollAdmin(int employeeID) throws Exception {
		super(employeeID);
		// TODO Auto-generated constructor stub
	}

	public PayrollAdmin(String username) throws Exception {
		super(username);
		// TODO Auto-generated constructor stub
	}

	public PayrollAdmin(String username, String password) throws Exception {
		// TODO Auto-generated constructor stub
		super(username, password);
	}

	// This function is complete
	public void changePayRate(int employeeID, double payRate) {
		db.update("employeeInfo", "payRate = " + payRate, "employeeID = "
				+ employeeID);
	}
	
	public double getMaxPay(int jobID)
	{
		double max = 0;
		try {
			ResultSet rs = db.select(" maxPay ", " pay "," jobID = " + jobID );
			while(rs.next())
			{
				max = rs.getDouble("maxPay");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return max;
	}
	
	public double getMinPay(int jobID)
	{
		double min = 0;
		try {
			ResultSet rs =db.select(" minPay ", " pay "," jobID = " + jobID );
			while(rs.next())
			{
				min = rs.getDouble("minPay");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return min;
	}
	
	// Functions but needs to be updated a little bit to change the hours part
	// of the date to morning and midnight for dateTime
	public double getOwedPay(int employeeID, DateTime start, DateTime end)
			throws Exception {
		double hours = 0;
		DateTimeFormatter dft = DateTimeFormat
				.forPattern("yyyy/MM/dd HH:mm:ss");
		ResultSet rs = db.select("hoursWorked", "hours", "employeeID = "
				+ employeeID + " AND startTime >= '" + start.toString(dft)
				+ "' AND startTime <= '" + end.toString(dft) + "'");
		while (rs.next()) {
			hours += rs.getDouble("hoursWorked");
		}
		Employee e = new Employee(employeeID);
		double rate = e.getPayRate();
		double owedPay = hours * rate;
		return owedPay;
	}
	
	public double getOwedPay(Employee e, DateTime start, DateTime end)
			throws Exception {
		double hours = 0;
		DateTimeFormatter dft = DateTimeFormat
				.forPattern("yyyy/MM/dd HH:mm:ss");
		ResultSet rs = db.select("hoursWorked", "hours", "employeeID = "
				+ employeeID + " AND startTime >= '" + start.toString(dft)
				+ "' AND startTime <= '" + end.toString(dft) + "'");
		while (rs.next()) {
			hours += rs.getDouble("hoursWorked");
		}
		
		double rate = e.getPayRate();
		double owedPay = hours * rate;
		return owedPay;
	}

	public void removeEmployee(int employeeID) {
		
		DateTimeFormatter dft = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
		DateTime curDate = new org.joda.time.DateTime();
		db.update("employeeInfo", "endDate = " + "'" + curDate.toString(dft)+"'", "employeeID = " + employeeID);
		db.delete("alerts", " employeeID = " + employeeID);
		db.delete("login", " employeeID = " + employeeID);
		
	}

	//This method is complete
	public void generateReport(Component comp, Vector<Integer> ids, DateTime start, DateTime end) throws Exception {
		final JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(comp);
		
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			// This is where a real application would open the file.
			FileWriter fw = new FileWriter(file);
			PrintWriter pw = new PrintWriter(fw);
			Iterator itr = ids.iterator();

			while(itr.hasNext())
			{
				int id = (int) itr.next();
				Employee e = new Employee(id);
				pw.print(e.getLastName());
				pw.print(",");
				pw.print(e.getFirstName());
				pw.print(",");
				pw.print(e.getEmployeeID());
				pw.print(",");
				pw.print(e.getAddress());
				pw.print(",");
				double pay = getOwedPay(id,start,end);
				pw.print(pay);
				pw.print(",");
				pw.println();				
			}
			
			// Flush the output to the file
			pw.flush();

			// Close the Print Writer
			pw.close();

			// Close the File Writer
			fw.close();
		} else {
			System.err.println("Something went wrong :( ");
		}

	}
	
	public void generateReport(Component comp, ArrayList<Employee> em, DateTime start, DateTime end) throws Exception {
		final JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(comp);
		
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			// This is where a real application would open the file.
			FileWriter fw = new FileWriter(file);
			PrintWriter pw = new PrintWriter(fw);

			for(int i = 0; i < em.size(); i++)
			{
				
				Employee e = em.get(i);
				pw.print(e.getLastName());
				pw.print(",");
				pw.print(e.getFirstName());
				pw.print(",");
				pw.print(e.getAddress());
				pw.print(",");
				pw.print(e.getEmployeeID());
				pw.print(",");
				double pay = getOwedPay(e,start,end);
				pw.print(pay);
				pw.print(",");
				pw.println();				
			}
			
			// Flush the output to the file
			pw.flush();

			// Close the Print Writer
			pw.close();

			// Close the File Writer
			fw.close();
		} else {
			System.err.println("Something went wrong :( ");
		}

	}
	
	@SuppressWarnings("null")
	public Collection<String> getList(Collection ids)
	{
		Collection<String> eInfo = new Vector<String>();
				
		try {
			ResultSet rs = db.select("", "employeeInfo", " endDate IS NULL ");
			while(rs.next())
			{
				String last = rs.getString("last_name");
				String first = rs.getString("first_name");
				int	id = rs.getInt("employeeID");
				ids.add(rs.getInt("employeeID"));
				String list = last+","+first+",("+id+")";
				eInfo.add(list);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return eInfo;
	}
	
	public void changeJobID (int employeeID, int newJobID) {
		db.update("employeeInfo", "jobID = " + newJobID, "employeeID = " + employeeID);
	}
	
	public static void main(String[] args) {
		try {

			DateTime start = new DateTime(2012, 11, 1, 15, 12, 00);
			DateTime end = new DateTime(2012, 12, 4, 23, 12, 00);

			PayrollAdmin P = new PayrollAdmin(2);
			
			P.changePayRate(1, 5.75);
			Component C = null;
			
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Collection<Integer> getMinions() throws Exception {
		ResultSet r = db.select("employeeID", "employeeInfo", "");
		
		Collection<Integer> eids = new ArrayList<Integer>();
		
		while(r.next())
			eids.add(r.getInt("employeeID"));
		
		return eids;
	}
}
